package org.farec.tcc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.farec.tcc.modelo.Permissao;
import org.farec.tcc.modelo.Usuario;

public class PermissaoDAO {
	
	private Connection connection;
	
	public PermissaoDAO(Connection connection)
	{
		this.connection = connection;
	}

	public List<Permissao> verificaPermissao(Usuario usuario, String ordem)
	{
		List<Permissao> p = new ArrayList<>();
		return p;
	}
	
	public List<Permissao> getLista()
	{
		try
		{
			PreparedStatement stmt = this.connection.prepareStatement("SELECT id, modulo from permissao order by modulo");
			ResultSet rs = stmt.executeQuery();
			List<Permissao> permissoes = new ArrayList<Permissao>();
			
			while(rs.next())
			{
				//Criando um nova instancia
				Permissao permissao = new Permissao();
				permissao.setCodigo(rs.getInt("id"));
				permissao.setModulo(rs.getString("modulo"));
									
				//Adicionando objeto a lista
				permissoes.add(permissao);
			}
			
			rs.close();
			stmt.close();
			return permissoes;
		}
		
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
	}

	public List<Permissao> verificaPermissao(int codEmpresa, Usuario usuario, String ordem)
	{
		ResultSet rs;
		try
		{
			/*
			 * O sql retorna a lista de permissoes do usuario que esteja associado empresa e a um determinado grupo
			 */
			String sql = "SELECT per.id, pg.modulo, item FROM permissao as per, permissaodogrupo as pg, usuario as us, empresa as emp "+ 
						 "WHERE per.modulo=pg.modulo and us.usuario=? and us.idempresa=emp.id and us.idempresa=? and pg.idgrupo=us.idgrupo order by "+ordem;
			
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			stmt.setString(1, usuario.getUsuario());
			stmt.setInt(2, codEmpresa);
			
			rs=stmt.executeQuery();
			
			List<Permissao> permissaoList = new ArrayList<Permissao>();
			
			while(rs.next())
			{
				//Criando um nova instancia
				Permissao permissao = new Permissao();
				
				permissao.setCodigo(rs.getInt("id"));
				permissao.setModulo(rs.getString("modulo"));
				permissao.setItem(rs.getString("item"));		
			
				permissaoList.add(permissao);
			}
			
			rs.close();
			stmt.close();
			return permissaoList;
		}
		
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	public List<Permissao> getPermissaoDoGrupo(Usuario usuario)
	{
		ResultSet rs;
		try
		{
			String sql = "SELECT t2.modulo from permissao as t1, permissaodogrupo as t2 where grupo=? and t1.modulo=t2.modulo and t2.permissao='true' ";
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			stmt.setString(1, usuario.getGrupo());
			rs=stmt.executeQuery();
			
			List<Permissao> permissoes = new ArrayList<Permissao>();
			
			while(rs.next())
			{
				//Criando um nova instancia
				Permissao permissao = new Permissao();
				permissao.setModulo(rs.getString("modulo"));
				//permissao.setUrl(rs.getString("url"));
				//permissao.setIcone(rs.getString("icone"));
				
				permissoes.add(permissao);
			}
			
			rs.close();
			stmt.close();
			return permissoes;
			
		}
		
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
	}

	
	
	public Permissao getPermissao(String nomePermissao, Permissao p)
	{
		Permissao permissao = new Permissao();
		String[] itens = {"adicionar","visualizar","alterar","remover", "historico","aprovacao","finalizar"};
		
		for(String item : itens)
		{
			if(p.getModulo().equals(nomePermissao) && p.getItem().equals(item))
			{
				permissao.setModulo(nomePermissao);
				permissao.setItem(item);
			}
		}
		
		return permissao;
	}
}

